#include <iostream>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <functional>
#include<pthread.h>
#include"Log.hpp"
#include"Terminal.hpp"
//./UdpClient 8080 0.0.0.0
void Usage(const std::string& proc)
{
    std::cout << "\n\rUsage: " << proc << " serverip serverport\n"
              << std::endl;
}

struct ThreadData
{
    struct sockaddr_in server;
    int sockfd;
    std::string serverip;
};
void *recv_message(void *args)
{
    OpenTerminal();
    ThreadData*td=static_cast<ThreadData*>(args);
    char inbuffer[SIZE];
    while(true)
    {
        //memset(inbuffer,0,sizeof(inbuffer));
        bzero(inbuffer,sizeof(inbuffer));
        struct sockaddr_in tmp;
        socklen_t len = sizeof(tmp);
        ssize_t n = recvfrom(td->sockfd, inbuffer, sizeof(inbuffer) - 1, 0, (sockaddr *)(&tmp), &len);
        if (n < 0)
        {
            perror("recvfrom error");
            return nullptr;
        }
        inbuffer[n] ='\0';
        std::cerr<<inbuffer<<std::endl;
        sleep(1);
    }
    


}

void *send_message(void *args)
{
    ThreadData*td=static_cast<ThreadData*>(args);
    std::string welcom=td->serverip;
    std::string message;
    welcom+= " comming...";
    socklen_t len=sizeof(td->server);
    sendto(td->sockfd,welcom.c_str(),welcom.size(),0,(sockaddr*)&td->server,len);
    sleep(1);
    while(true)
    {
        std::cout << "Please Enter@ ";
        getline(std::cin, message);
        // 1. 数据 2. 给谁发
        sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&(td->server), len);
    }

}
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::string serverip=argv[1];
    uint16_t serverport=std::stoi(argv[2]);

    //struct sockaddr_in server;
    ThreadData td;
    bzero(&td.server,sizeof(td.server));
    td.server.sin_family=AF_INET;
    td.server.sin_port=htons(serverport);
    td.server.sin_addr.s_addr=inet_addr(serverip.c_str());
    socklen_t len = sizeof(td.server);

    td.sockfd=socket(AF_INET,SOCK_DGRAM,0);
    if(td.sockfd<0)
    {
        std::cout << "socker error" << std::endl;
        return 1;
    }
    // client 要bind吗？要！只不过不需要用户显示的bind！一般有OS自由随机选择！
    // 一个端口号只能被一个进程bind，对server是如此，对于client，也是如此！
    // 其实client的port是多少，其实不重要，只要能保证主机上的唯一性就可以！
    // 系统什么时候给我bind呢？首次发送数据的时候
    // std::string message;
    // char inbuffer[SIZE];
    // while(true)
    // {
    //     std::cout<<"Please Enter@:";
    //     getline(std::cin,message);
        
    //     sendto(td.sockfd,message.c_str(),message.size(),0,(struct sockaddr*)&td.server,len);

    //     struct sockaddr_in tmp;
    //     bzero(&tmp,sizeof(tmp));
    //     socklen_t len1=sizeof(tmp);
    //     ssize_t s=recvfrom(td.sockfd,inbuffer,sizeof(inbuffer)-1,0,(struct sockaddr*)&tmp,&len1);
    //     if(s > 0)
    //     {
    //         inbuffer[s] = 0;
    //         std::cout << inbuffer << std::endl;
    //     }
    td.serverip=serverip;
    pthread_t  recvr, sender;
    pthread_create(&recvr,0, recv_message,&td);
    pthread_create(&sender,0, send_message,&td);
        
    pthread_join(recvr, nullptr);
    pthread_join(sender, nullptr);

    close(td.sockfd);
    return 0;
}
